﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

namespace Yunchang.ILUI
{
    [LuaCallCSharp]
    [Hotfix]
    public class UIPage : UIItem
    {
        /// <summary>
        /// Page 的depth 厚度
        /// </summary>
        public const int Depth_THICKNESS = 30;
        
        public UILayer uiLayer { get; protected set; }
        public TablePage table { get; protected set; }
        /// <summary>
        /// 表ID
        /// </summary>
        public string ID
        {
            get { return table != null ? table.id : ""; }
        }

        public int mStartDepth = 0;
        /// <summary>
        /// 开始深度
        /// </summary>
        public int startDepth
        {
            get { return mStartDepth; }
            private set
            {
                mStartDepth = value;
                UpdateDepth();
            }
        }
       /// <summary>
       /// 最大深度
       /// </summary>
        public int maxDepth { get; private set; }
        /// <summary>
        /// 层组中的索引ID
        /// </summary>
        public int stackIndex { get; private set; }
        public int depth
        {
            get { return mCanvas.sortingOrder; }
            private set { mCanvas.sortingOrder = value; }
        }

        protected Canvas mCanvas = null;

        Action<string> CloseSelf = null;
        Action<UIItem> OnComplete = null;
        Action<UIPage, bool> activeChange;


        public void Open(
            TablePage mTable,
            UILayer tempLayer,
            Action<UIItem> OnComplete,
            Action<string> mCloseSelf,
            Action<UIPage, bool> activeChange,
            object arg)
        {
            this.table = mTable;
            this.uiLayer = tempLayer;
            this.resourceID = mTable.ResourceID;
            this.OnComplete = OnComplete;
            this.CloseSelf = mCloseSelf;
            this.activeChange = activeChange;
            this.mArg = arg;
            if (inited)
            {
                OnRefresh(arg);
            }
            else if(OnOpen(arg))
            {
                Load();
            }
        }

        /// <summary>
        /// 界面打开的时候
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public virtual bool OnOpen(object arg)
        {
            if (luaCtrl != null)
            {
                LuaFunction ac = luaCtrl.Get<LuaFunction>("OnOpen");
                if (ac != null)
                {
                    return ac.Func<object, bool>(arg);
                }
            }
            return true;
        }

        /// <summary>
        /// 执行加载显示逻辑
        /// </summary>
        protected void Load()
        {
            LoadAsset(uiLayer.uiStackTransform, OnComplete);
        }
        protected override void OnInit()
        {
            InitCanvas();
            base.OnInit();
            OnShow(mArg);
            OnRefresh(mArg);
        }

        public override void OnEnable()
        {
            base.OnEnable();
            activeChange(this, true);
            Debug.Log("UIPage OnEnable :"+ID);
        }
        public override void OnDisable()
        {
            base.OnDisable();
            activeChange(this, false);
            Debug.Log("UIPage OnDisable :" + ID);
        }
        protected void InitCanvas()
        {
            tranform.anchorMin = Vector2.zero;
            tranform.anchorMax = Vector2.one;
            tranform.offsetMin = Vector2.zero;
            tranform.offsetMax = Vector2.zero;
            tranform.localScale = Vector2.one;

            mCanvas = uiMono.GetComponent<Canvas>();
            if (mCanvas == null)
            {
                GLog.Error("UIPage not find Canvas ! id = {0} gameObject.name = {1}",ID,uiMono.name);
                return;
            }

            canvas.overrideSorting = true;
            trunk = true;
            uiLayer.PushUIPage(this);
        }

        public virtual void OnRefresh(object args)
        {
            this.mArg = args;
            if (luaCtrl != null)
            {
                if (args != null)
                    CallLua("OnRefresh", args);
                else
                    CallLua("OnRefresh");
            }
        }

        public virtual void OnShow(object arg)
        {
            this.mArg = arg;
            if (luaCtrl != null)
            {
                if (arg != null)
                    CallLua("OnShow", arg);
                else
                    CallLua("OnShow");
            }
        }

        public void Close()
        {
            if (CloseSelf != null)
                CloseSelf(ID);
        }

        public void SetStackIndex(int index, bool sort)
        {
            stackIndex = index;
            this.startDepth = GetDepthInLayer;
        }

        public int GetDepthInLayer
        {
            get { return uiLayer.StartPanelDepth + stackIndex * Depth_THICKNESS; }
        }

        public override void UpdateDepth()
        {
            if (orderList == null) return;
            int count = orderList.Count;
            for (int i = 0; i < count; i++)
            {
                if (orderList[i] != null)
                {
                    orderList[i].sortingOrder = mStartDepth + i;
                }
            }

            maxDepth = mStartDepth + count;
        }

        public void UpdateUIOrderList()
        {
            uiMono.MakeData();
            UpdateDepth();
        }

        public void ExciteDestroy()
        {
            GameObject.Destroy(gameObject);
        }
    }
}